package com.atguigu.flink.day08;

import com.atguigu.flink.beans.WaterSensor;
import com.atguigu.flink.func.WaterSensorMapFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Felix
 * @date 2023/12/9
 * 该案例演示了键控状态-ListState
 * 需求：针对每种传感器输出最高的3个水位值
 */
public class Flink02_KeyedState_ListState {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        SingleOutputStreamOperator<WaterSensor> wsDS = env
            .socketTextStream("hadoop102", 8888)
            .map(new WaterSensorMapFunction());

        //按照传感器的id进行分组
        KeyedStream<WaterSensor, String> keyedDS = wsDS.keyBy(WaterSensor::getId);

        //对分组后的数据进行处理
        keyedDS.process(
            new KeyedProcessFunction<String, WaterSensor, String>() {
                //声明状态
                private ListState<Integer> vcListState;
                //初始化状态
                @Override
                public void open(Configuration parameters) throws Exception {
                    ListStateDescriptor<Integer> listStateDescriptor
                        = new ListStateDescriptor<Integer>("vcListState",Integer.class);
                    vcListState = getRuntimeContext().getListState(listStateDescriptor);
                }

                @Override
                public void processElement(WaterSensor ws, Context ctx, Collector<String> out) throws Exception {
                    //获取当前这条数据水位值
                    Integer vc = ws.vc;
                    //将这条水位值放到状态中
                    vcListState.add(vc);
                    //从状态中将所有的水位值取出来
                    Iterable<Integer> it = vcListState.get();
                    //定义一个List集合用于排序
                    List<Integer> vcList = new ArrayList<>();
                    for (Integer vc1 : it) {
                        vcList.add(vc1);
                    }
                    //排序
                    vcList.sort((o1, o2) -> o2 - o1);
                    if(vcList.size() > 3){
                        vcList.remove(3);
                    }
                    //输出
                    out.collect("传感器id：" + ctx.getCurrentKey() +"水位值最大的"+vcList.toString());
                    //更新状态
                    vcListState.update(vcList);
                }
            }
        ).print();

        env.execute();
    }
}
